package cn.itcast.solr;

import cn.itcast.pojo.Heiheima;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

import java.util.List;


public class SolrIndexQueryTest {

    //查询
    @Test
    public void testQuery() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");
        
        //查询对象
        SolrParams query=new SolrQuery("title:手机");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        SolrDocumentList results = queryResponse.getResults();
        for (SolrDocument document : results) {
            String id= (String) document.get("id");
            String title= (String) document.get("title");
            Long price= (Long) document.get("price");
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    //查询使用pojo
    @Test
    public void testQueryWithBean() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
        SolrParams query=new SolrQuery("title:手机");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    /**
     * 模糊查询 ~
     *相似程度 默认最大编辑距离2 修改在后面加数字就可以
     */

    @Test
    public void testLikeQueryWithBean() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
        SolrParams query=new SolrQuery("title:vovu~");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    /**
     * 范围查询
     * 注：两个边界都是包含的
     * @throws SolrServerException
     */
    @Test
    public void testRangQueryWithBean() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
//        SolrParams query=new SolrQuery("price:[200000 TO 300000]");
//        SolrParams query=new SolrQuery("price:[199999 TO 300000]");
        SolrParams query=new SolrQuery("price:[* TO 300000]");
//        SolrParams query=new SolrQuery("price:[200000 TO *]");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        System.out.println("一共查到:"+list.size()+"条");
        System.out.println("=======================");
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    /**
     * 组合查询
     *
     * NOT
     * AND
     * OR
     * @throws SolrServerException
     */
    @Test
    public void testBooleanQueryWithBean() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
//        SolrParams query=new SolrQuery("price:[200000 TO 300000]");
//        SolrParams query=new SolrQuery("price:[199999 TO 300000]");
        SolrParams query=new SolrQuery("price:[* TO 300000] NOT title:iphone AND title:小米" );
//        SolrParams query=new SolrQuery("price:[200000 TO *]");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        System.out.println("一共查到:"+list.size()+"条");
        System.out.println("=======================");
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }
}
